package com.styletransfer.service;


import com.styletransfer.exception.BusinessException;
import com.styletransfer.mapper.UserMapper;
import com.styletransfer.model.ApiResponse;
import com.styletransfer.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserFollowService {

    private static final Logger logger = LoggerFactory.getLogger(UserFollowService.class);

    @Autowired
    private UserMapper userMapper;

    /**
     * 关注用户
     */
    @Transactional
    public ApiResponse<Void> followUser(Long userId, Long followingId) {
        try {
            // 检查是否关注自己
            if (userId.equals(followingId)) {
                throw new BusinessException("不能关注自己");
            }

            // 检查要关注的用户是否存在
            User followingUser = userMapper.findById(followingId);
            if (followingUser == null) {
                throw new BusinessException("要关注的用户不存在");
            }

            // 检查是否已经关注
            if (userMapper.checkFollowing(userId, followingId)) {
                throw new BusinessException("已经关注过该用户");
            }

            // 添加关注关系
            userMapper.addFollowing(userId, followingId);

            return ApiResponse.success();
        } catch (BusinessException e) {
            return ApiResponse.error(e.getMessage());
        } catch (Exception e) {
            logger.error("关注用户失败: " + e.getMessage(), e);
            return ApiResponse.error("关注失败");
        }
    }

    /**
     * 取消关注用户
     */
    @Transactional
    public ApiResponse<Void> unfollowUser(Long userId, Long followingId) {
        try {
            // 检查是否已经关注
            if (!userMapper.checkFollowing(userId, followingId)) {
                throw new BusinessException("未关注该用户");
            }

            // 取消关注关系
            userMapper.removeFollowing(userId, followingId);

            return ApiResponse.success();
        } catch (BusinessException e) {
            return ApiResponse.error(e.getMessage());
        } catch (Exception e) {
            logger.error("取消关注用户失败: " + e.getMessage(), e);
            return ApiResponse.error("取消关注失败");
        }
    }

    /**
     * 检查是否已关注用户
     */
    public ApiResponse<Boolean> checkFollowing(Long userId, Long followingId) {
        try {
            boolean isFollowing = userMapper.checkFollowing(userId, followingId);
            return ApiResponse.success(isFollowing);
        } catch (Exception e) {
            logger.error("检查关注状态失败: " + e.getMessage(), e);
            return ApiResponse.error("检查关注状态失败");
        }
    }

    /**
     * 获取用户的关注列表
     */
    public ApiResponse<List<User>> getFollowing(Long userId) {
        try {
            List<User> following = userMapper.getFollowing(userId);
            return ApiResponse.success(following);
        } catch (Exception e) {
            logger.error("获取关注列表失败: " + e.getMessage(), e);
            return ApiResponse.error("获取关注列表失败");
        }
    }

    /**
     * 获取用户的粉丝列表
     */
    public ApiResponse<List<User>> getFollowers(Long userId) {
        try {
            List<User> followers = userMapper.getFollowers(userId);
            return ApiResponse.success(followers);
        } catch (Exception e) {
            logger.error("获取粉丝列表失败: " + e.getMessage(), e);
            return ApiResponse.error("获取粉丝列表失败");
        }
    }
}
